﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StockCrawlerLib.DAL
{
    public class StockRepository
    {
        private StockDataClassesDataContext dc = new StockDataClassesDataContext();
        
        public bool IsExists(string code)
        {
            bool result = false;

            var obj = from p in dc.stocks
                      where p.code.Contains(code)
                      select p;

            if (obj.Count() > 0)
            {
                result = true;
            }
            return result;
        }

        public void Insert(string code, string name, string search, int stockexchange)
        {
            if (!IsExists(code))
            {
                stock obj = new stock();
                obj.code = code;
                obj.name = name;
                obj.search = search;
                obj.id_stockexchange = stockexchange;
                obj.isRemark = false;
                Insert(obj);
            }
        }

        public void Insert(stock obj)
        {
            dc.stocks.InsertOnSubmit(obj);
            dc.SubmitChanges();
        }

        public void Update(stock obj)
        {
            dc.Refresh(RefreshMode.KeepChanges, obj);
            dc.SubmitChanges(ConflictMode.ContinueOnConflict);
        }

        public void UpdateIsRemark(int id_stock, bool isRemark)
        {
            stock obj = dc.stocks.Single(p => p.id_stock == id_stock);
            obj.isRemark = isRemark;

            dc.Refresh(RefreshMode.KeepChanges, obj);
            dc.SubmitChanges(ConflictMode.ContinueOnConflict);
        }

        public IQueryable<stock> SelectRecordsByStockExchange(int id_stockexchange)
        {
            var query = from p in dc.stocks
                        where p.id_stockexchange == id_stockexchange
                        select p;
            return query;
        }

        //public IQueryable<stock> Select(string code)
        //{
        //    var query = from p in dc.stocks
        //                where p.code == code
        //                select p;
        //    return query;
        //}

        public stock Select(string code)
        {
            stock obj = dc.stocks.Single(p=>p.code == code);
            return obj;
        }
        public IQueryable<stock> SelectAllRecords()
        {
            var query = from p in dc.stocks
                        select p;
            return query;
        }

        /// <summary>
        /// Load 3 san Hose, Ha, Upcom
        /// </summary>
        /// <returns></returns>
        public IQueryable<stock> GetStockList()
        {
            var query = from p in dc.stocks
                        where (p.id_stockexchange == 1 || p.id_stockexchange == 2 || p.id_stockexchange == 9)
                        && p.status == true
                        select p;
            return query;
        }

        public IQueryable GetDefaultStockList()
        {
            var query = from p in dc.stocks
                        where (p.id_stockexchange == 1 || p.id_stockexchange == 2 || p.id_stockexchange == 9)
                        && p.status == true
                        select new
                         {
                             p.id_stock,
                             p.code,
                             p.stockexchange.stockexchange_name,
                             p.name,
                             p.pe,
                             p.eps,
                             p.bv,
                             p.klcp_luuhanh,
                             p.klcp_niemyet,
                             p.vcsh,
                             p.isRemark
                         };
            return query;
        }

        /// <summary>
        /// Lấy stock được remark
        /// </summary>
        /// <returns></returns>
        public IQueryable GetRemarkStockList()
        {
            var query = from p in dc.stocks
                        where (p.id_stockexchange == 1 || p.id_stockexchange == 2 || p.id_stockexchange == 9)
                        && p.status == true && p.isRemark == true
                        select new
                        {
                            p.id_stock,
                            p.code,
                            p.stockexchange.stockexchange_name,
                            p.name,
                            p.pe,
                            p.eps,
                            p.bv,
                            p.klcp_luuhanh,
                            p.klcp_niemyet,
                            p.vcsh,
                            p.isRemark
                        };
            return query;
        }

        /// <summary>
        /// Lấy những stock mới lên sàn trong khoảng thời gian < numDays
        /// </summary>
        /// <param name="numDays">Khoảng thời gian lên sàn</param>
        /// <returns></returns>
        public IQueryable GetNewStockList(int numDays)
        {
            var query = from p in dc.stocks
                        where (p.id_stockexchange == 1 || p.id_stockexchange == 2 || p.id_stockexchange == 9)
                        && p.status == true && p.histories.Count < numDays && p.histories.Count > 0
                        select new
                        {
                            p.id_stock,
                            p.code,
                            p.stockexchange.stockexchange_name,
                            p.name,
                            p.pe,
                            p.eps,
                            p.bv,
                            p.klcp_luuhanh,
                            p.klcp_niemyet,
                            p.vcsh,
                            p.isRemark
                        };
            return query;
        }


    }
}
